/**
 * https://leetcode-cn.com/problems/subarray-sum-equals-k/
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
 var subarraySum = function(nums, k) {
    const sumCount = new Map();
    sumCount.set(0, 1);
    const sums = [0];

    let count = 0;
    nums.forEach((num, i) => {
        const sum = i === 0 ? num : (sums[i] + num);
        const target = sum - k;
        count += (sumCount.get(target) || 0);
        sums.push(sum);
        sumCount.set(sum, (sumCount.get(sum) || 0) + 1);
    });
    return count;
};
